import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { Global } from "../../../../../com/gengine/global/Global";
import { FrameTimer } from "../../../../../com/gengine/core/frame/FrameTimer";
import { Cache } from "../../../cache/Cache";
import { Language } from "../../../../../extend/language/Language";
import { LoaderManager } from "../../../../../com/gengine/resource/LoaderManager";
import { ObjCreate } from "../../common/ObjCreate";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { ImagesConst } from "../../../resource/ImagesConst";
import { SixtyDefenceBuilding } from "./SixtyDefenceBuilding";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { ClockManager } from "../../../manager/ClockManager";
import { SecTimer } from "../../../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../../../com/gengine/core/frame/TimerType";
import { ImageInfo } from "../../../../../com/gengine/resource/info/ImageInfo";
import { SWFInfo } from "../../../../../com/gengine/resource/info/SWFInfo";
import { SCopyDefenceInfo } from "../../../../../Message/Public/SCopyDefenceInfo";
import { BossConfig } from "../../../resource/BossConfig";
import { RemainTimeTool } from "../../award/RemainTimeTool";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { GLabel } from "../../../../../com/mui/controls/GLabel";
type int = number;
//class SixtyDefenceInfoPanel
    
    export  class SixtyDefenceInfoPanel /* flash.display.Sprite */
    {
        constructor()
        {
            
            this._yellowTfSmall = new GTextFormat(FontUtil.songtiName, 12, 16776960, null, null, null, null, null, flash.text.TextFormatAlign.CENTER);
            this._yellowTfBig = new GTextFormat(FontUtil.songtiName, 14, 16776960, null, null, null, null, null, flash.text.TextFormatAlign.CENTER);
            this._bossBuildingDic = new Map<any, any> /* flash.utils.Dictionary */();
            this._bosses = [];
            this.createChildren();
            return;
        }

        /* internal  */onMoveMouseDown(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (arg1.target == this._bodySprit) 
            {
                this.startDrag();
                Global.stage.addEventListener(flash.events.MouseEvent.MOUSE_UP, this.onStageMouseUpHandler);
            }
            return;
        }

        /* internal  */onEnterFrameHandler(arg1: FrameTimer): void
        {
            var loc1=Cache.instance.copy.defendCopyExp;
            this._lastExp = this._lastExp + this._addExpEach;
            if (this._lastExp > loc1) 
            {
                this._lastExp = loc1;
            }
            this._expLabel.setStyle("textFormat", this._yellowTfBig);
            this._expLabel.text = Language.getString(60096) + this._lastExp;
            if (this._lastExp >= loc1) 
            {
                if (this._frame && this._frame.running) 
                {
                    this._frame.stop();
                }
            }
            return;
        }

        /* internal  */createChildren(): void
        {
            this._bodySprit = new Object /* flash.display.Sprite */();
            this._bodySprit.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onMoveMouseDown);
            this.addChild(this._bodySprit);
            this._ringLabelBgBig = new Object /* flash.display.Bitmap */();
            this._ringLabelBgSmall = new Object /* flash.display.Bitmap */();
            LoaderManager.instance.load("SixtyDefenceCopy_CountBg.png", this.onRingBgBigLoaded);
            LoaderManager.instance.load("copy_countdownRing_bg2.png", this.onRingBgSmallLoaded);
            this._timeLabel = ObjCreate.createLabel("", 0, 5, 296, 30, flash.text.TextFormatAlign.CENTER, this._bodySprit);
            this._timeLabel.mouseEnabled = false;
            this._timeLabel.mouseChildren = false;
            this._expLabel = ObjCreate.createLabel("", 0, 30, 296, 30, flash.text.TextFormatAlign.CENTER, this._bodySprit, true, 16776960);
            this._expLabel.toolTipData = HTMLUtil.addColor(Language.getString(60095), "#FFFFFF");
            LoaderManager.instance.load("copy_countdownRing_bottle.swf", this.onRingBottleLoaded);
            this._buildingSprit = new Object /* flash.display.Sprite */();
            this._bodySprit.addChild(this._buildingSprit);
            ObjCreate.createBg(0, 52, 296, 2, this, ImagesConst.Line);
            this._materialLabel = ObjCreate.createLabel("", 10, 60, 280, 20, flash.text.TextFormatAlign.LEFT, this._buildingSprit, true, 16776960);
            this._materialLabel.toolTipData = HTMLUtil.addColor(Language.getString(60370), "#FFFFFF");
            var loc1=0;
            while (loc1 < 4) 
            {
                this.createBuilding(8 + loc1 * 70, 78, loc1 + 1);
                ++loc1;
            }
            this._subBtn = ObjCreate.createGButton("", 265, 3, 24, 22, null, "SubBtn2");
            this._subBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onSubpButtonClick);
            this._supBtn = ObjCreate.createGButton("", 265, 3, 24, 22, null, "SupBtn2");
            this._supBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onSubpButtonClick);
            this.updateStatus();
            return;
        }

        /* internal  */onStageMouseUpHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.stopDrag();
            Global.stage.removeEventListener(flash.events.MouseEvent.MOUSE_UP, this.onStageMouseUpHandler);
            return;
        }

        /* internal  */onSubpButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.target;
            switch (loc1) 
            {
                case this._subBtn:
                {
                    this._status = this.SMALL;
                    this.updateStatus();
                    break;
                }
                case this._supBtn:
                {
                    this._status = this.BIG;
                    this.updateStatus();
                    break;
                }
            }
            return;
        }

        /* internal  */createBuilding(arg1: int, arg2: int, arg3: int): SixtyDefenceBuilding
        {
            var loc1=null;
            (loc1 = new SixtyDefenceBuilding()).x = arg1;
            loc1.y = arg2;
            loc1.bossType = arg3;
            this._buildingSprit.addChild(loc1);
            this._bossBuildingDic[arg3] = loc1;
            return loc1;
        }

        /* internal  */getBuilding(arg1: int): SixtyDefenceBuilding
        {
            return this._bossBuildingDic[arg1] as SixtyDefenceBuilding;
        }

        public setCopyCode(arg1: int): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=this._bossBuildingDic;
            for(loc1 of loc3) 
            {
                loc1.copyCode = arg1;
            }
            return;
        }

        public setTimeLabel_Ring(arg1: int=-1, arg2: int=-1, arg3: string="环数"): void
        {
            var loc1=0;
            this._timerType = arg3;
            this._timeLabel.setStyle("textFormat", this._yellowTfSmall);
            this._timeLabel.textField.filters = [FilterConst.glowFilter];
            if (this._timerType != this.RING) 
            {
                if (this._timerType == this.BOSS_REFRESH) 
                {
                    this._bosses.push({"bossId": arg1, "refreshTime": ClockManager.instance.nowDate.time + arg2 * 1000});
                    loc1 = Math.round((this._bosses[0]["refreshTime"] - ClockManager.instance.nowDate.time) / 1000);
                    this._timerNumSec = loc1;
                }
            }
            else 
            {
                if (this._ring != -1) 
                {
                    this._ring = arg1;
                }
                this.setLabelRing();
            }
            if (this._timerType != this.RING) 
            {
                if (!this._timer) 
                {
                    this._timer = new SecTimer();
                    this._timer.addListener(TimerType.ENTERFRAME, this.onTimerHandler);
                }
                this._timer.start();
            }
            return;
        }

        /* internal  */setLabelRing(): void
        {
            var loc1=Math.floor((this._ring + 4) / 5);
            this._timeLabel.text = Language.getStringByParam(60092, loc1);
            if (this._ring != 1) 
            {
                if (this._ring == 400) 
                {
                    this._timeLabel.text = Language.getString(60094);
                }
            }
            else 
            {
                this._timeLabel.text = Language.getString(60093);
            }
            return;
        }

        /* internal  */onRingBgBigLoaded(arg1: ImageInfo): void
        {
            this._ringLabelBgBig.bitmapData = arg1.bitmapData;
            if (this._status == this.BIG) 
            {
                this.addChildAt(this._ringLabelBgBig, 0);
            }
            return;
        }

        /* internal  */onRingBgSmallLoaded(arg1: ImageInfo): void
        {
            this._ringLabelBgSmall.bitmapData = arg1.bitmapData;
            if (this._status == this.SMALL) 
            {
                this.addChildAt(this._ringLabelBgSmall, 0);
            }
            return;
        }

        /* internal  */onRingBottleLoaded(arg1: SWFInfo): void
        {
            if (!this._ringLabelBottle && arg1.clip) 
            {
                this._ringLabelBottle = arg1.clip;
                this._ringLabelBottle.x = -220;
                this._ringLabelBottle.y = -160;
                this._ringLabelBottle.mouseChildren = false;
                this._ringLabelBottle.mouseEnabled = false;
                this._bodySprit.addChildAt(this._ringLabelBottle, 1);
                this.updateRingBottleExp();
            }
            return;
        }

        public updateBuildingInfo(arg1: SCopyDefenceInfo): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            this._materialLabel.htmlText = Language.getStringByParam(60369, arg1.score);
            var loc4=0;
            var loc5=this._bossBuildingDic;
            for (loc2 in loc5) 
            {
                loc3 = parseInt(loc2);
                loc1 = this.getBuilding(loc3);
                if (!loc1) 
                {
                    continue;
                }
                if (arg1.copyInfo[loc3] == null) 
                {
                    loc1.bossLevel = 0;
                }
                else 
                {
                    loc1.bossLevel = arg1.copyInfo[loc3];
                }
                if (arg1.bossCountInfo[loc3] != null) 
                {
                    loc1.bossNum = arg1.bossCountInfo[loc3];
                    continue;
                }
                loc1.bossNum = 0;
            }
            return;
        }

        public updateRingBottleExp(): void
        {
            var loc1=NaN;
            var loc2=0;
            if (Cache.instance.copy.defendCopyExp - this._lastExp >= 10000) 
            {
                this._addExpEach = 1111;
            }
            else if (Cache.instance.copy.defendCopyExp - this._lastExp >= 5000) 
            {
                this._addExpEach = 111;
            }
            else 
            {
                this._addExpEach = 11;
            }
            if (!this._frame) 
            {
                this._frame = new FrameTimer();
                this._frame.addListener(TimerType.ENTERFRAME, this.onEnterFrameHandler);
            }
            if (!this._frame.running) 
            {
                this._frame.start();
            }
            if (this._ringLabelBottle) 
            {
                if (Cache.instance.copy.copyInfo && Cache.instance.copy.copyInfo.code == 124) 
                {
                    loc1 = Cache.instance.copy.defendCopyExp / Cache.instance.copy.defendMaxExpSingle;
                }
                else if (Cache.instance.copy.copyInfo && Cache.instance.copy.copyInfo.code == 125) 
                {
                    loc1 = Cache.instance.copy.defendCopyExp / Cache.instance.copy.defendMaxExpGroup;
                }
                loc1 = loc1 > 1 ? 1 : loc1;
                loc2 = Math.round(this._ringLabelBottle.totalFrames * loc1);
                this._ringLabelBottle.gotoAndStop(loc2);
            }
            return;
        }

        public setBossRefreshLabel(): void
        {
            var loc1=0;
            var loc2=null;
            if (this._bosses.length > 0) 
            {
                loc1 = Math.round((this._bosses[0]["refreshTime"] - ClockManager.instance.nowDate.time) / 1000);
                if (loc1 <= 0) 
                {
                    this._bosses.pop();
                    if (this._bosses.length > 0) 
                    {
                        this._timerNumSec = Math.round((this._bosses[0]["refreshTime"] - ClockManager.instance.nowDate.time) / 1000);
                    }
                }
                if (this._bosses.length > 0) 
                {
                    loc2 = BossConfig.instance.getInfoByCode(this._bosses[0]["bossId"] as int);
                    this._timeLabel.text = Language.getStringByParam(60098, loc2.boss.name, RemainTimeTool.getRemainTimeBySeconds(this._timerNumSec));
                }
                else 
                {
                    this._timeLabel.text = Language.getString(60099);
                }
            }
            else 
            {
                this._timeLabel.text = Language.getString(60100);
            }
            return;
        }

        /* internal  */onTimerHandler(arg1: SecTimer): void
        {
            if (this._timerType == this.BOSS_REFRESH) 
            {
                this.setBossRefreshLabel();
            }
            --this._timerNumSec;
            if (this._timerNumSec < 0 && this._bosses.length == 0) 
            {
                this._timer.stop();
            }
            return;
        }

        public stopTimer(): void
        {
            if (this._timer && this._timer.running) 
            {
                this._timer.stop();
            }
            return;
        }

        public dispose(): void
        {
            if (this._timer) 
            {
                this._timer.stop();
                this._timer.dispose();
                this._timer = null;
            }
            if (this._frame) 
            {
                if (this._frame.running) 
                {
                    this._frame.stop();
                }
                this._frame = null;
            }
            return;
        }

        public updateStatus(): void
        {
            if (this._status != this.BIG) 
            {
                if (this._status == this.SMALL) 
                {
                    this._bodySprit.x = 97;
                    this._timeLabel.width = 199;
                    this._expLabel.width = 199;
                    this._ringLabelBgSmall.x = 97;
                    this.addChildAt(this._ringLabelBgSmall, 0);
                    if (this._ringLabelBgBig.parent) 
                    {
                        this._ringLabelBgBig.parent.removeChild(this._ringLabelBgBig);
                    }
                    if (this._buildingSprit.parent) 
                    {
                        this._buildingSprit.parent.removeChild(this._buildingSprit);
                    }
                    this.addChild(this._supBtn);
                    if (this._subBtn.parent) 
                    {
                        this._subBtn.parent.removeChild(this._subBtn);
                    }
                }
            }
            else 
            {
                this._bodySprit.x = 0;
                this._timeLabel.width = 296;
                this._expLabel.width = 296;
                this.addChildAt(this._ringLabelBgBig, 0);
                if (this._ringLabelBgSmall.parent) 
                {
                    this._ringLabelBgSmall.parent.removeChild(this._ringLabelBgSmall);
                }
                if (!this._buildingSprit.parent) 
                {
                    this._bodySprit.addChild(this._buildingSprit);
                }
                this.addChild(this._subBtn);
                if (this._supBtn.parent) 
                {
                    this._supBtn.parent.removeChild(this._supBtn);
                }
            }
            return;
        }

        public updatePosition(): void
        {
            if (this._ringLabelBottle) 
            {
                this._ringLabelBottle.x = -220;
                this._ringLabelBottle.y = -160;
                this._bodySprit.addChildAt(this._ringLabelBottle, 1);
            }
            return;
        }

        public /* const */RING: string="环数";

        public /* const */BOSS_REFRESH: string="boss刷新";

        public /* const */BIG: string="big";

        public /* const */SMALL: string="small";

        /* internal */ /* var */_buildingSprit: Object /* flash.display.Sprite */;

        /* internal */ /* var */_subBtn: GButton;

        /* internal */ /* var */_supBtn: GButton;

        /* internal */ /* var */_yellowTfSmall: Object /* flash.text.TextFormat */;

        /* internal */ /* var */_ring: int;

        /* internal */ /* var */_bosses: Array<any>;

        /* internal */ /* var */_yellowTfBig: Object /* flash.text.TextFormat */;

        /* internal */ /* var */_ringLabelBgBig: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_ringLabelBgSmall: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_ringLabelBottle: Object /* flash.display.MovieClip */;

        /* internal */ /* var */_timeLabel: GLabel;

        /* internal */ /* var */_expLabel: GLabel;

        /* internal */ /* var */_addExpEach: int=11;

        /* internal */ /* var */_lastExp: int;

        /* internal */ /* var */_materialLabel: GLabel;

        /* internal */ /* var */_timer: SecTimer;

        /* internal */ /* var */_timerNumSec: int;

        /* internal */ /* var */_timerType: string;

        /* internal */ /* var */_frame: FrameTimer;

        /* internal */ /* var */_bossBuildingDic: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_bodySprit: Object /* flash.display.Sprite */;

        /* internal */ /* var */_status: string="big";
    }
